let router = require('../router/Router');
let storeService = require('../service/StoreService');

let agentService = require('../service/AgentService');

const { BusiError, SysError, ErrorResult } = require('../exception/exception');


//  roomID := params["roomID"].(string)
// 	gameType := params["gameType"].(string)
// 	roomTs := int64(params["roomTs"].(float64))
//
// 	err = putSettlement(roomID, gameType, roomTs, params)

router.post("/game/settlement/save", (req, resp) => {
    storeService.saveSettlement(req.body.data).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("save settlement err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/settlement/query", (req, resp) => {
    storeService.querySettlement(req.body).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/settlement/item/save", (req, resp) => {
    storeService.saveSettlementItem(req.body.data).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("save settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/settlement/item/query", (req, resp) => {
    storeService.querySettlementItem(req.uid, req.body.gameType).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/settlement/query", (req, resp) => {
    storeService.querySettlementItemWithID(req.body.id).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//俱乐部大战绩
router.post("/auth/game/settlement/querybyclub", (req, resp) => {
    storeService.querySettlementItemWithClub(req.uid,req.body.clubid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//代理
//查本人
router.post("/auth/game/settlement/queryAgentByClubMy", (req, resp) => {
    storeService.queryAgentWithClubMy(req.uid,req.body.clubid,req.body.tsdate).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("queryAgentByClubMy err:", err.message);
        resp.send(new ErrorResult(err));
    });
});
//查下级
router.post("/auth/game/settlement/queryAgentByClubLower", (req, resp) => {
    storeService.queryAgentWithClubLower(req.uid,req.body.clubid,req.body.tsdate).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("queryAgentByClubMy err:", err.message);
        resp.send(new ErrorResult(err));
    });
});
//查详细
router.post("/auth/game/settlement/queryAgentByClubDetailed", (req, resp) => {
    storeService.queryAgentWithClubDetailed(req.body.clubid,req.body.paiJuNo).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("queryAgentByClubMy err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//用户的战绩
router.post("/settlement/querybyuid", (req, resp) => {
    storeService.queryMemberSettlementItem(req.body.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//俱乐部详细战绩
router.post("/auth/game/settlement/querybyclub/details", (req, resp) => {
    console.logw("玩家发来数据 ", req.body.roomid,req.body.roomts);
    storeService.querySettlementItemWithClubDetails(req.uid,req.body.roomid,req.body.roomts).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//联盟主战绩
router.post("/auth/game/settlement/querybyunion", (req, resp) => {
    storeService.queryUnion(req.body.unionid,req.body.tsdate).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});
//联盟详细战绩
router.post("/auth/game/settlement/querybyunion/details", (req, resp) => {
    storeService.queryUnionDetails(req.body.unionid,req.body.tsdate).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});
//联盟玩家主战绩
router.post("/auth/game/settlement/querybyunionplay", (req, resp) => {
    storeService.queryUnionPlay(req.uid,req.body.unionid,req.body.tsdate).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});
//玩家详细战绩
router.post("/auth/game/settlement/querybyunionplay/details", (req, resp) => {
    storeService.queryUnionPlayDetails(req.uid,req.body.unionid,req.body.tsdate).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//房间详细战绩
router.post("/auth/game/settlement/querybyunionroom", (req, resp) => {
    console.logw("玩家发来数据 ", req.body.roomid,req.body.roomts);
    storeService.queryUnionRoomDetails(req.body.roomid,req.body.roomts).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/record/save", (req, resp) => {
    let mydata = req.body.data;
    agentService.playerScore(mydata.clubID,mydata.uid,mydata.gameType,mydata.tax,mydata.roomID);
    //设置局里类型，区分俱乐部
    if(mydata.clubID === '5740934'){
        mydata.juType = '3';
    }
    storeService.saveGameRecord(mydata).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("save game record err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//大联盟详细战绩
router.post("/game/record/save/union", (req, resp) => {
    storeService.saveGameUnionRecord(req.body.data).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("save game record err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//add
router.post("/game/record/save/room", (req, resp) => {
    //add async playerScore(clubId,uid,gameType,tax) {
    let mydata = req.body.data;
    if(mydata.clubID === '5740934'){
        resp.send({code: 200, msg: "success"});
        return;
    }
    storeService.saveRoomRecord(req.body.data).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("save game record err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/record/save/room/union", (req, resp) => {
    storeService.saveRoomUnionRecord(req.body.data).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("save game record err:", err.message);
        resp.send(new ErrorResult(err));
    });
});


router.post("/auth/game/record/query", (req, resp) => {
    storeService.saveGameRecord(req.body.data).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query game record err:", err.message);
        resp.send(new ErrorResult(err));
    });
});


